package com.server.llh.llhserverwork.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;


import com.llh.llhsuppliers.R;
import com.server.llh.llhserverwork.bean.Constants;
import com.server.llh.llhserverwork.bean.SerInitProPackage;

import com.server.llh.llhserverwork.port.OptionServerInterface;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 服务项显示
 */
public class LLHServerCateAdapter extends BaseAdapter{
    List<SerInitProPackage> listData;

    OptionServerInterface serverInterface;
    public List<SerInitProPackage> getListData() {
        return listData;
    }
    public void setListData(List<SerInitProPackage> listData) {
        this.listData = listData;
    }
    public void registerCallBack(OptionServerInterface serverInterfaced) {
        serverInterface = serverInterfaced;
    }
    String[] titleArr;
    private Context context;
    public LLHServerCateAdapter(Context context, List<SerInitProPackage> serviceInitProPack) {
        this.context=context;
        this.listData=serviceInitProPack;
        this.titleArr = context.getResources().getStringArray(R.array.unit_name);
    }
    @Override
    public int getCount() {
        if (listData == null || listData.size() == 0) {
            return 0;
        }
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.server_cate_item_option, parent, false);
            holder = new ViewHolder();
            holder.server_image = (ImageView) convertView
                    .findViewById(R.id.server_image);
            holder.server_name = (TextView) convertView
                    .findViewById(R.id.server_name);
            holder.server_org = (RatingBar) convertView
                    .findViewById(R.id.server_org);
            holder.server_price = (TextView) convertView
                    .findViewById(R.id.server_price);
            holder.server_miaos = (TextView) convertView
                    .findViewById(R.id.server_miaos);
            holder.select = (TextView) convertView
                    .findViewById(R.id.select);
            holder.select_shoping = (TextView) convertView
                    .findViewById(R.id.select_shoping);
            holder.tv_num = (TextView) convertView
                    .findViewById(R.id.im_reduce);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final SerInitProPackage infoData = listData.get(position);
        holder.server_name.setText(infoData.getPackName());
        holder.server_price.setText("¥"+infoData.getPrice()+"元/"+titleArr[infoData.getUnit()].toString());
        holder.server_miaos.setText(infoData.getReamark());
        holder.tv_num.setText("付款");
        holder.tv_num.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                infoData.buyNum=1;
                if (infoData!=null){
                    serverInterface.callBack(infoData);
                }
            }
        });
        if (infoData.getPictureUrl()!=null) {
            Picasso.with(context).load(Constants.SER_IMAGE_URL+infoData.getPictureUrl()).into( holder.server_image);
        } else {
            holder.server_image.setImageResource(R.drawable.icon_launcher);
        }
        return convertView;
    }
    class ViewHolder{
        ImageView server_image;
        TextView server_name,server_price,server_miaos,select,select_shoping,tv_num;
        RatingBar server_org;

    }

}
